package com.hospital.hospitalsystem.modules.doctor.controller;

import com.hospital.hospitalsystem.base.BaseResponse;
import com.hospital.hospitalsystem.base.PageData;
import com.hospital.hospitalsystem.config.TokenVerify;
import com.hospital.hospitalsystem.modules.doctor.biz.DoctorBiz;
import com.hospital.hospitalsystem.modules.doctor.dto.*;
import com.hospital.hospitalsystem.modules.hospital.dto.DoctorGetReq;
import com.hospital.hospitalsystem.modules.login.biz.LoginBiz;
import com.hospital.hospitalsystem.utils.LoginUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author qiancheng@cqxyy.net
 * @date 2023/2/24
 */
@RestController
@RequestMapping("/doctor")
@Api(tags = "医生管理")
@Validated
public class DoctorController {

    @Autowired
    private DoctorBiz doctorBiz;

    @Autowired
    private LoginBiz loginBiz;

    @Autowired
    private LoginUtils loginUtils;

    @ApiOperation(value = "医生查看预约信息")
    @RequestMapping(path = "/book/list",method = RequestMethod.POST)
    @TokenVerify(type = "doctor,hospital")
    public BaseResponse<PageData<DoctorBookRsp>> bookList(@RequestBody DoctorBookReq req){
        if (loginUtils.getUser().getRoles().equals("doctor")){
            req.setDoctorUuid(loginUtils.getUser().getUserUuid());
        } else if (loginUtils.getUser().getRoles().equals("hospital")){
            req.setHospitalUuid(loginUtils.getUser().getUserUuid());
        }
        PageData<DoctorBookRsp> result = doctorBiz.bookList(req);
        return BaseResponse.success(result);
    };

    @ApiOperation(value = "医生完成预约")
    @RequestMapping(path = "/book/finish",method = RequestMethod.POST)
    @TokenVerify(type = "doctor")
    public BaseResponse bookFinish(@RequestBody DoctorFinishReq req){
        req.setDoctorUuid(loginUtils.getUser().getUserUuid());
        doctorBiz.bookFinish(req);
        return BaseResponse.success();
    };

    @ApiOperation(value = "医生填写个人信息")
    @RequestMapping(path = "/introduce/add",method = RequestMethod.POST)
    @TokenVerify(type = "doctor,hospital")
    public BaseResponse introduceAdd(@RequestBody DoctorIntroduceReq req){
        if (loginUtils.getUser().getRoles().equals("doctor")){
            req.setDoctorUuid(loginUtils.getUser().getUserUuid());
        } else {
            req.setHospitalUuid(loginUtils.getUser().getUserUuid());
        }
        doctorBiz.introduce(req);
        return BaseResponse.success();
    };

    @ApiOperation(value = "医生查看个人信息")
    @RequestMapping(path = "/introduce/show",method = RequestMethod.POST)
    @TokenVerify(type = "doctor,hospital,user")
    public BaseResponse introduceShow(@RequestBody DoctorIntroduceReq req){
        if (loginUtils.getUser().getRoles().equals("doctor")){
            req.setDoctorUuid(loginUtils.getUser().getUserUuid());
        } else if(loginUtils.getUser().getRoles().equals("hospital")){
            req.setHospitalUuid(loginUtils.getUser().getUserUuid());
        }
        DoctorIntroduceRsp result = doctorBiz.introduceShow(req);
        return BaseResponse.success(result);
    };

    @ApiOperation(value = "医生修改个人信息")
    @RequestMapping(path = "/introduce/update",method = RequestMethod.POST)
    @TokenVerify(type = "doctor,hospital")
    public BaseResponse introduceUpdate(@RequestBody DoctorIntroduceReq req){
        if (loginUtils.getUser().getRoles().equals("doctor")){
            req.setDoctorUuid(loginUtils.getUser().getUserUuid());
        } else {
            req.setHospitalUuid(loginUtils.getUser().getUserUuid());
        }
        doctorBiz.introduceUpdate(req);
        return BaseResponse.success();
    };

    @ApiOperation(value = "医生叫号检查")
    @RequestMapping(path = "/number-check",method = RequestMethod.POST)
    @TokenVerify(type = "doctor")
    public BaseResponse numberCheck(@RequestBody DoctorNumberCheckReq req){
        req.setDoctorUuid(loginUtils.getUser().getUserUuid());
        DoctorNumberCheckRsp result = doctorBiz.numberCheck(req);
        return BaseResponse.success(result);
    };

    @ApiOperation(value = "医生叫号")
    @RequestMapping(path = "/number",method = RequestMethod.POST)
    @TokenVerify(type = "doctor")
    public BaseResponse number(@RequestBody DoctorNumberCheckReq req){
        req.setDoctorUuid(loginUtils.getUser().getUserUuid());
        DoctorNumberRsp result = doctorBiz.number(req);
        return BaseResponse.success(result);
    };

    @ApiOperation(value = "医生开始问诊")
    @RequestMapping(path = "/start",method = RequestMethod.POST)
    @TokenVerify(type = "doctor")
    public BaseResponse start(@RequestBody DoctorStartReq req){
        req.setDoctorUuid(loginUtils.getUser().getUserUuid());
        DoctorNumberRsp result = doctorBiz.start(req);
        return BaseResponse.success(result);
    };

    @ApiOperation(value = "根据bookUuid获取用户信息")
    @RequestMapping(path = "/message-bookUuid",method = RequestMethod.POST)
    @TokenVerify(type = "doctor")
    public BaseResponse messageBookUuid(@RequestBody MessageBookUuidReq req){
        req.setDoctorUuid(loginUtils.getUser().getUserUuid());
        MessageBookUuidRsp result = doctorBiz.messageBookUuid(req);
        result.setUserAge(loginBiz.getAgeFromIdCard(result.getUserIdCard()));
        return BaseResponse.success(result);
    };

    @ApiOperation(value = "医生获取预约时刻表")
    @RequestMapping(path = "/get-book-time",method = RequestMethod.POST)
    @TokenVerify(type = "doctor")
    public BaseResponse getBookTime(@RequestBody DoctorGetReq req){
        req.setDoctorUuid(loginUtils.getUser().getUserUuid());
        List<GetBookTimeRsp> result = doctorBiz.getBookTime(req);
        return BaseResponse.success(result);
    };
}
